import { createRouter, createWebHashHistory } from 'vue-router'
import { storeToRefs } from 'pinia';

import { useUserInfoStore } from '@/stores/userinfoStrore'
import Layout from '../layout/index.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/user/login.vue'),
      meta: {
        title: '登录注册',
        disable: false,
      }
    },
    {
      path: '/404',
      name: '404',
      component: () => import('@/views/error/404.vue'),
      meta: {
        title: '404',
        disable: false,
      },
    },
    {
      path: '/401',
      name: '401',
      component: () => import('@/views/error/401.vue'),
      meta: {
        title: '401',
        disable: false,
      },
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404',
    },
    {
      path: '/',
      name: 'index',
      component: Layout,
      redirect: "index",
      meta: {
        title: '首页',
        disable: true,
        icon: "icon-shouye",
        lang: "Home"
      },
      children: [
        {
          path: '/',
          name: 'index',
          component: () => import('@/views/index/index.vue'),
          meta: {
            title: "首页",
            disable: false
          },
        },
      ]
    },
    {
      path: '/system-management',
      name: 'system-management',
      component: Layout,
      meta: {
        title: "系统管理",
        disable: true,
        icon: "icon-xitongweihu",
        lang: "SystemManagement"
      },
      children: [
        {
          path: '/system-management/user-list',
          name: 'user-list',
          component: () => import('@/views/system-management/user-list.vue'),
          meta: {
            title: "用户列表",
            disable: true,
            lang: "UserList"
          },
        },
        {
          path: '/system-management/role-list',
          name: 'role-list',
          component: () => import('@/views/system-management/role-list.vue'),
          meta: {
            title: "角色管理",
            disable: true,
            lang: "RoleManagement"
          },
        },
        {
          path: '/system-management/menu-list',
          name: 'menu-list',
          component: () => import('@/views/system-management/menu-list.vue'),
          meta: {
            title: "菜单管理",
            disable: true,
            lang: "MenuManagement"
          },
        },
        {
          path: '/system-management/ad-list',
          name: 'ad-list',
          component: () => import('@/views/system-management/ad-list.vue'),
          meta: {
            title: "广告管理",
            disable: true,
            lang: "AdManagement"
          },
        },
        {
          path: '/system-management/user-info',
          name: 'user-info',
          component: () => import('@/views/system-management/user-info.vue'),
          meta: {
            title: "个人中心",
            disable: false
          },
        },
      ]
    },
    {
      path: '/dictionary',
      name: 'dictionary',
      component: Layout,
      redirect: "/dictionary/index",
      meta: {
        title: "字典",
        disable: true,
        icon: "icon-zidian",
        lang: "Dictionary"
      },
      children: [
        {
          path: '/dictionary/index',
          name: 'dictionary',
          component: () => import('@/views/dictionary/index.vue'),
          meta: {
            title: "字典",
            disable: true
          },
        },
      ]
    },
    {
      path: '/classification',
      name: 'classification',
      component: Layout,
      redirect: "/classification/index",
      meta: {
        title: "分类",
        disable: true,
        icon: "icon-fenlei",
        lang: "Classification"
      },
      children: [
        {
          path: '/classification/index',
          name: 'classification',
          component: () => import('@/views/classification/index.vue'),
          meta: {
            title: "分类",
            disable: true
          },
        },
      ]
    },
    {
      path: '/resource',
      name: 'resource',
      component: Layout,
      redirect: "/resource/index",
      meta: {
        title: "资源",
        disable: true,
        icon: "icon-wangluoziyuanfuwu",
        lang: "Resource"
      },
      children: [
        {
          path: '/resource/index',
          name: 'resource',
          component: () => import('@/views/resource/index.vue'),
          meta: {
            title: "资源",
            disable: true,
            icon: "icon-wangluoziyuanfuwu",
            lang: "Resource"
          },
        },
        {
          path: '/resource/site-inclusion',
          name: 'site-inclusion',
          component: () => import('@/views/resource/site-inclusion.vue'),
          meta: {
            title: "站点收录",
            disable: true,
            lang: "SiteInclusion"
          },
        },
      ]
    },
    // {
    //   path: '/article-blog',
    //   name: 'article-blog',
    //   component: Layout,
    //   redirect: "/article-blog/index",
    //   meta: {
    //     title: "文章博客",
    //     disable: true,
    //     icon: "icon-kb2",
    //     lang: "ArticleBlog"
    //   },
    //   children: [
    //     {
    //       path: '/article-blog/index',
    //       name: 'article-blog',
    //       component: () => import('@/views/article-blog/index.vue'),
    //       meta: {
    //         title: "文章博客",
    //         disable: true
    //       },
    //     },
    //   ]
    // },
  ],
  // 控制滚动条的位置
  scrollBehavior() {
    return {
      left: 0,
      top: 0
    }
  }
})
router.beforeEach((to, from, next) => {
  const { loginStatus } = useUserInfoStore();
  // 登录页面且未登录。跳转到登录页
  if (to.path === '/login') return next();
  // 没有登录，重定向到登录页面
  if (!loginStatus.state) return next(`/login?redirect=${to.path}`);
  next();
})

export default router
